#include "addfriendwidget.h"

//////////////////////////////////
/// 一个搜索结果
//////////////////////////////////
FriendResultItem::FriendResultItem(const UserInfo &userInfo)
    :_userInfo(userInfo)
{
    // 设置自身属性
    this->setFixedHeight(70);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 创建布局管理器
    _layout = new QGridLayout();
    _layout->setHorizontalSpacing(5);
    _layout->setVerticalSpacing(0);
    _layout->setContentsMargins(10, 5, 10, 5);
    _layout->setAlignment(Qt::AlignTop);
    this->setLayout(_layout);

    // 创建头像
    _avatarBtn = new QPushButton();
    _avatarBtn->setFixedSize(50, 50);
    _avatarBtn->setIconSize(QSize(50, 50));
    _avatarBtn->setIcon(_userInfo._avatar);

    // 创建昵称
    _nameLabel = new QLabel();
    _nameLabel->setFixedHeight(35);
    _nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    _nameLabel->setStyleSheet("QLabel { font-size: 16px; font-weight: 700; }");
    _nameLabel->setText(_userInfo._nickname);

    // 创建签名
    _descLabel = new QLabel();
    _descLabel->setFixedHeight(35);
    _descLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _descLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    _descLabel->setStyleSheet("QLabel { font-size: 12px; }");
    _descLabel->setText(_userInfo._desciption);

    // 创建添加按钮
    _addBtn = new QPushButton();
    _addBtn->setFixedSize(100, 40);
    _addBtn->setText("添加好友");
    QString btnStyle = "QPushButton { border: none; background-color: rgb(137, 217, 97); color: rgb(255, 255, 255); border-radius: 10px; }";
    btnStyle += "QPushButton:pressed { background-color: rgb(200, 200, 200); }";
    _addBtn->setStyleSheet(btnStyle);

    // 添加到布局管理器中
    _layout->addWidget(_avatarBtn, 0, 0, 2, 1);
    _layout->addWidget(_nameLabel, 0, 1);
    _layout->addWidget(_descLabel, 1, 1);
    _layout->addWidget(_addBtn, 0, 2, 2, 1);
}

//////////////////////////////////
/// 添加好友主界面
//////////////////////////////////
AddFriendWidget::AddFriendWidget(QWidget *parent)
    :QDialog(parent)
{
    // 设置基本属性
    this->setFixedSize(500, 500);
    this->setWindowTitle("添加好友");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QDialog { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 添加布局管理器
    _layout = new QGridLayout();
    _layout->setSpacing(5);
    _layout->setContentsMargins(10, 5, 10, 5);
    _layout->setAlignment(Qt::AlignTop);
    this->setLayout(_layout);

    // 创建搜索框
    _searchEdit = new QLineEdit();
    _searchEdit->setFixedHeight(50);
    _searchEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    QString style = "QLineEdit { border: none; border-radius: 10px; background-color: rgb(240, 240, 240); padding-left: 5px; }";
    _searchEdit->setStyleSheet(style);
    _searchEdit->setPlaceholderText("输入 昵称/用户id/邮箱 进行搜索");

    _layout->addWidget(_searchEdit, 0, 0, 1, 8);

    // 创建搜索按钮
    _searchBtn = new QPushButton();
    _searchBtn->setFixedSize(50, 50);
    _searchBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    _searchBtn->setIconSize(QSize(30, 30));
    _searchBtn->setIcon(QIcon(":resource/image/search.png"));
    QString btnStyle = "QPushButton { border: none; background-color: rgb(240, 240, 240); border-radius: 10px; }";
    btnStyle += "QPushButton:hover { background-color: rgb(225, 225, 225) } QPushButton:pressed { background-color: rgb(195, 195, 195); }";
    _searchBtn->setStyleSheet(btnStyle);

    _layout->addWidget(_searchBtn, 0, 8, 1, 1);

    // 添加滚动区域
    initResultArea();

#if TEST_UI
    UserInfo userInfo;
    userInfo._avatar = QIcon(":/resource/image/defaultAvatar.png");
    userInfo._desciption = "你好你好你好你好";
    for (int i = 0; i < 20; i++)
    {
        userInfo._user_id = QString::number(1000 + i);
        userInfo._nickname = "张三" + QString::number(i);
        addResult(userInfo);
    }
#endif
}

void AddFriendWidget::setSearchKey(const QString &key)
{
    _searchEdit->setText(key);
}

void AddFriendWidget::initResultArea()
{
    // 创建滚动区域
    _scrollArea = new QScrollArea();
    _scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    _scrollArea->setWidgetResizable(true);
    _scrollArea->setStyleSheet("QScrollArea { border: none; }");
    _scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0px; }");
    _scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 2px; background-color: rgb(255, 255, 255); }");

    _layout->addWidget(_scrollArea, 1, 0, 1, 9);

    // 创建滚动区域的widget
    _container = new QWidget();
    _container->setObjectName("container");
    _container->setStyleSheet("#container { background-color: rgb(255, 255, 255); }");
    _scrollArea->setWidget(_container);

    // 给widget添加布局管理器
    _vlayout = new QVBoxLayout();
    _vlayout->setSpacing(5);
    _vlayout->setContentsMargins(0, 0, 0, 0);
    _vlayout->setAlignment(Qt::AlignTop);
    _container->setLayout(_vlayout);
}


void AddFriendWidget::addResult(const UserInfo &userInfo)
{
    FriendResultItem *friendResultItem = new FriendResultItem(userInfo);
    _vlayout->addWidget(friendResultItem);
}

void AddFriendWidget::clear()
{
    for (int i = _vlayout->count() - 1; i >= 0; i++)
    {
        QLayoutItem *item = _vlayout->takeAt(i);
        if (item == nullptr || item->widget() == nullptr) continue;
        delete item->widget();
    }
}

